package com.example.news.ui;



import android.os.Bundle;

import android.util.Log;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import androidx.recyclerview.widget.OrientationHelper;
import androidx.recyclerview.widget.RecyclerView;


import com.cjj.MaterialRefreshLayout;
import com.example.news.Video.Girl;
import com.example.news.Video.MyLayoutManager;
import com.example.news.Video.VideoAdapter;
import com.example.news.R;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;


public class VideoFragment extends Fragment {

    ExecutorService executor = Executors.newFixedThreadPool(2);
    ThreadPoolExecutor pool = (ThreadPoolExecutor) executor;
    private RecyclerView recyclerView;
    private VideoAdapter videoAdapter;
    private MyLayoutManager LayoutManager;
    private MaterialRefreshLayout materialRefreshLayout;

    private List<Girl> list = new ArrayList<>();
    private View view;
    OkHttpClient okHttpClient = new OkHttpClient();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
         view = inflater.inflate(R.layout.videofragment,container,false);

        for (int i =0; i < 20; i++){
            pool.execute(new task());

        }

        iniREC();
        return view;
    }


    private void iniREC(){
        recyclerView = view.findViewById(R.id.rv_main_video);
        videoAdapter = new VideoAdapter(getActivity(),list);
        LayoutManager = new MyLayoutManager(getActivity(), OrientationHelper.VERTICAL, false);
        recyclerView.setAdapter(videoAdapter);
        recyclerView.setLayoutManager(LayoutManager);

//        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
//            @Override
//            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
//                super.onScrolled(recyclerView, dx, dy);
//
//            }
//        });

//        materialRefreshLayout.setMaterialRefreshListener(new MaterialRefreshListener() {
//            @Override
//            public void onRefresh(MaterialRefreshLayout materialRefreshLayout) {
//                for (int i =0; i < 2; i++){
//                    pool.execute(new task());
//                }
//                //acgAdapter.notifyDataSetChanged();
//            }
//
//            @Override
//            public void onRefreshLoadMore(MaterialRefreshLayout materialRefreshLayout) {
//                for (int i =0; i < 2; i++){
//                    pool.execute(new task());
//                }
//                //acgAdapter.notifyDataSetChanged();
//            }
//        });
    }

    private class task implements Runnable{

        @Override
        public void run() {
            try {
                TimeUnit.MILLISECONDS.sleep(300);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            Request request = new Request.Builder()
                    .url("http://i.yixuedh.com/api/xjjvideo.php")
                    .build();
            Call call = okHttpClient.newCall(request);
            call.enqueue(new Callback() {
                @Override
                public void onFailure(@NotNull Call call, @NotNull IOException e) {

                }

                @Override
                public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                    String result = response.body().string();
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            list.add(new Girl(result));
                            videoAdapter.notifyDataSetChanged();
                        }
                    });

                }
            });

        }
    }


    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        Log.e("TAG", "ThreeFragment onHiddenChanged :" + hidden);
    }


}
